package uvt.map.graph.algorithm.result;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.TreeMap;

import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

import uvt.gui.mainframe.object.InfoFrame;
import uvt.map.PositionsSettings;
import uvt.map.ShowElement;
import uvt.map.graph.algorithm.Algorithm;
import uvt.map.graph.interfaces.FromWithOnToElement;

public class LoadingFrame extends JFrame implements PropertyChangeListener {
 
    private ProgressMonitor progressMonitor;
    private Task task;
 
    class Task extends SwingWorker<Void, Void> {
        @Override
        public Void doInBackground() {
            setProgress(0);
            try {
            	Algorithm alg = new Algorithm();
            	if(!alg.checkIfTransportationAvailable())
            		throw new Exception("No transportation at this hour!");
                Thread.sleep(1000);
                simulateWait(10,0);
                ShowElement.showGraph(PositionsSettings.getInstance().getMap());
                simulateWait(30,10);
                alg.positionVehiclesOnMap();
                simulateWait(100,30);
                new ResultFrame(alg.getListRoutes());
            } catch(final Exception ex) {
            	Thread t = new Thread(new Runnable() {
    				public void run() {
    					new InfoFrame("Error",ex.getMessage());
    			    }
    			});
    			t.start();
            }
            return null;
        }
 
        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            progressMonitor.close();
        }
        
        public void simulateWait(int val,int progress) {
        	Random random = new Random();
        	try {
        		while (progress < val && !isCancelled()) {
                    //Sleep for up to one second.
                    Thread.sleep(random.nextInt(700));
                    //Make random progress.
                    progress += random.nextInt(10);
                    setProgress(Math.min(progress, 100));
                }
        	} catch(Exception ex) {
        		ex.printStackTrace();
        	}
        }
    }
 
    public LoadingFrame(Component component) {
    	progressMonitor = new ProgressMonitor(component,
								              "Searching for routes ...",
								              "aaaa", 0, 100);
		progressMonitor.setProgress(0);
		task = new Task();
		task.addPropertyChangeListener(this);
		task.execute();
    }
 
    public void actionPerformed(ActionEvent evt) {
        
    }
 
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName() ) {
            int progress = (Integer) evt.getNewValue();
            progressMonitor.setProgress(progress);
            String message = String.format("Completed %d%%.\n", progress);
            progressMonitor.setNote(message);
            if (progressMonitor.isCanceled() || task.isDone()) {
                Toolkit.getDefaultToolkit().beep();
                if (progressMonitor.isCanceled()) {
                    task.cancel(true);
                } 
            }
        }
 
    }
    
}